/* authType 权限类型
* 0:普通用户（拥有crm权限）
* 1:普通媒介用户（拥有媒介库权限，不能使用全部导出，不拥有crm权限）
* 2:高级媒介用户（拥有媒介库权限，可以使用全部导出，不拥有crm权限）
* 3:内品管理员（拥有内品管理权限，可以使用管理、导入、删除，不拥有crm权限）
* 4:内品用户（拥有内品查看权限，含标识符标识权限的负责人，可以管理和查看，非负责人可以查看，不拥有crm权限）
*/
import store from '../store';

// 权限列表
export const USER_TYPES = {
  'SUPER_ADMIN': -1,
  'CRM': 0,
  'MEDIUM': 1,
  'MEDIUM_ADMIN': 2,
  'BTAND_ADMIN': 3,
  'BTAND': 4
};
// 路由权限列表，控制侧边菜单
export const ROUTER_TYPES = {
  'CRM': 'crm', // crm
  'SALEMANAGE': [ // 销售管理
    'crm:orderList:read', 'crm:customer:', 'crm:sales:', 'medium:supplier:', 'medium:supplierAudit:', 'crm:project:read', 'crm:project:admin', 'crm:cost:read'
  ],
  'COMMERCE': [ // 电商
    'crm:goodsList:read',
    'crm:goodsOrder:read',
    'crm:commerceSupplier:read'
  ],
  'CRM_ORDER': 'crm:orderList:read',
  'CRM_FINANCE_SEALINGACCOUNT': [
    'crm:finance:sealAccountRead', 'crm:finance:submitSealAmount', 'crm:finance:sellFirstTrial', 'crm:finance:financeRecheck', 'crm:finance:financeReport'
  ], // 财务封账
  'CRM_CUSTOMER': 'crm:customer:',
  'CRM_RESOURCE': 'crm:resource:',
  'CRM_SALES': 'crm:sales:',
  'CRM_USERLIST': 'crm:userList:',
  'CRM_INVOICE': 'crm:receipt:',
  'CRM_SUBCRIBE': 'crm:subcribe:',

  'AUTH': 'auth', // 权限模块
  'AUTH_ROLE': 'auth:role:',
  'AUTH_OPERATOR': 'auth:operator:',
  'AUTH_LOG': 'auth:log:',

  'MEDIUM': 'medium:', // 媒介库
  'MEDIUM_RESOURCE': ['medium:resource:', 'medium:platform:', 'medium:operator:', 'medium:dataEdit:', 'medium:dataShow:'],
  'MEDIUMORDER': ['medium:sale:', 'medium:medium:'], // 媒介库

  'BRAND': 'brand:', // 经营平台
  'BRAND_ACCOUNT': 'brand:account:',
  'BRAND_ACCOUNTCODELIST': 'brand:accountCodeList:',

  'TOTAL': [ // 销售业务流
    'total:report:', 'total:financereport:', 'total:brandreport:', 'total:mediumreport:', 'chart:departEdit:read'
  ],
  'TOTAL_FIELD': 'total:field:',
  'TOTAL_REPORT': ['total:report:', 'total:financereport:', 'total:brandreport:', 'total:mediumreport:'],

  'SYSTEM': ['auth:system:', 'system:ipSeries:read', 'system:cost:read']
};
// 超级管理员
export function isSuperAdmin() {
  return hasPermission(USER_TYPES.SUPER_ADMIN);
}

// CRM普通用户
export function isCRMNormal() {
  return hasPermission(USER_TYPES.CRM);
}

// 媒介库权限
export function isMediumUser() {
  return hasPermission([USER_TYPES.MEDIUM, USER_TYPES.MEDIUM_ADMIN]);
}
export function isMediumNormal() {
  return hasPermission(USER_TYPES.MEDIUM);
}
export function isMediumAdmin() {
  return hasPermission(USER_TYPES.MEDIUM_ADMIN);
}

// 内品权限
export function isBrandUser() {
  return hasPermission([USER_TYPES.BTAND_ADMIN, USER_TYPES.BTAND]);
}
export function isBrandNormal() {
  return hasPermission(USER_TYPES.BTAND);
}
export function isBrandAdmin() {
  return hasPermission(USER_TYPES.BTAND_ADMIN);
}

export function transeToRoles(authType) {
  let roles;
  if (!authType) { // 空默认
    roles = [USER_TYPES.CRM];
  } else if (typeof authType === 'number') { // 数字 有值
    roles = [authType];
  } else if (typeof authType === 'string') { // 字符串 有值
    const number = Number(authType);
    roles = number ? [number] : authType.split(',').map(i => Number(i));
  } else { // 数字数组，如: [1, 2, 3]
    roles = authType;
  }
  return roles;
}

export default function hasPermission(roles = [], userRoles = store.getters.roles) {
  if (typeof roles === 'number') {
    return roles === USER_TYPES.SUPER_ADMIN || userRoles.some(item => item === roles);
  } else {
    return userRoles.some(item => item === USER_TYPES.SUPER_ADMIN || roles.some(i => i === item));
  }
}
// 判断路由是否显示在侧边栏
export function hasRouteAuth(routeRole = [], userRoles = store.getters.roles) {
  if (typeof routeRole === 'string') {
    return userRoles.includes(routeRole);
  } else {
    return userRoles.some(item => {
      if (typeof item === 'number') {
        return item === USER_TYPES.SUPER_ADMIN;
      } else if (typeof item === 'string') {
        return routeRole.some(i => item.indexOf(i) >= 0);
      } else {
        return false;
      }
    });
  }
}
